package course.p4_list.s2_TwoNextList;

import course.p4_list.ListTest;
import org.junit.Test;

import static course.p4_list.util.Random.numberGenerator;

/**
 * 双向链表测试类
 */
public class TestDemo implements ListTest {

    @Test
    @Override
    public void LengthTest() {
        Node[] listHead = generatorBothWayNode(10,1,20);
        System.out.println("链表长度为："+ DLinkedListAlgorithm.ListLengthByHead(listHead[0]));
        DLinkedListAlgorithm.toStringByHead(listHead[0]);
        System.out.println("链表长度为："+ DLinkedListAlgorithm.ListLengthByTail(listHead[1]));
        DLinkedListAlgorithm.toStringByTail(listHead[1]);
    }

    @Test
    @Override
    public void InsertTest() {
        Node[] listHead = generatorBothWayNode(10,1,20);
        Node test1 = new Node(21,null);
        Node test2 = new Node(22,null);
        System.out.println("变更前:");
        DLinkedListAlgorithm.toStringByHead(listHead[0]);
        System.out.println("正向插入变更后:");
        DLinkedListAlgorithm.toStringByHead(DLinkedListAlgorithm.insertByHead(listHead[0],test1,10));

        Node[] listHead2 = generatorBothWayNode(10,1,20);
        System.out.println("变更前:");
        DLinkedListAlgorithm.toStringByTail(listHead2[1]);
        System.out.println("反向向插入变更后:");
        DLinkedListAlgorithm.toStringByTail(DLinkedListAlgorithm.insertByTail(listHead2[1],test2,10));
    }

    @Test
    @Override
    public void DeleteTest() {
        Node[] listHead= generatorBothWayNode(10,1,20);
        System.out.println("删除前：");
        DLinkedListAlgorithm.toStringByHead(listHead[0]);
        System.out.println("正向删除后");
        DLinkedListAlgorithm.toStringByHead(DLinkedListAlgorithm.deleteByHead(listHead[0],9));

        Node[] listHead2 = generatorBothWayNode(10,1,20);
        System.out.println("删除前：");
        DLinkedListAlgorithm.toStringByHead(listHead2[0]);
        System.out.println("反向删除后");
        DLinkedListAlgorithm.toStringByTail(DLinkedListAlgorithm.deleteByTail(listHead2[1],0));
    }

    @Test
    public void getTest(){
        Node[] listHead = generatorBothWayNode(10,1,20);
        DLinkedListAlgorithm.toStringByHead(listHead[0]);
        System.out.println(DLinkedListAlgorithm.getByHead(listHead[0],3).data);
        System.out.println(DLinkedListAlgorithm.getByTail(listHead[1],2).data);
    }

    /**
     * 自动随机生成双向链表，用于测试
     * @param length 链表长度
     * @param min 链表最小值
     * @param max 链表最大值
     * @return 生成的链表头节点和尾节点组成的数组，index=0 head ; index=1 tail
     */
    public static Node[] generatorBothWayNode(int length, int min, int max){
        Node headNode=null;
        Node nextNode=null;
        Node[] listNode = new Node[2];
        for(int i=0;i<length;i++){
            int num = numberGenerator(min,max);
            if(headNode==null){
                headNode= new course.p4_list.s2_TwoNextList.Node(num,null);
                nextNode=headNode;
                listNode[0]=headNode;
            }else{
                Node node=new course.p4_list.s2_TwoNextList.Node(num,null);
                nextNode.next = (node);
                node.previous = (nextNode);
                nextNode=node;
            }
        }
        listNode[1]=nextNode;
        return listNode;
    }


}
